﻿/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.entity.Company;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;




@Controller
public class SearchProjectController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	private SearchProjectService searchProjectService;

	private SearchProject searchProject;
	java.lang.Integer id = null;
	private String[] items;

	/** 通过spring自动注入 */
	public void setSearchProjectServiceImpl(SearchProjectService service) {
		this.searchProjectService = service;
	}

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response, SearchProjectQuery searchProjectQuery) throws IOException {
		OnlineManager onlineManager = super.getOnlineManager(request, response);
		if (onlineManager != null) {
			OnlineUser onlineUser = onlineManager.getOnlineUser();
			searchProjectQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectService.findPage(searchProjectQuery);

		List<SearchProject> SearchProjectlist = (List) page.getResult();
		ListRange<SearchProject> resultList = new ListRange<SearchProject>();
		resultList.setRows(SearchProjectlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	/**
	 * 联系记录
	 * 
	 * @param request
	 * @param response
	 * @param searchProjectQuery
	 * @throws IOException
	 */
	public void contactrecordlist(HttpServletRequest request, HttpServletResponse response, ContactRecordQuery contactRecordQuery) throws IOException {
		OnlineManager onlineManager = super.getOnlineManager(request, response);
		if (onlineManager != null) {
			OnlineUser onlineUser = onlineManager.getOnlineUser();
			contactRecordQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectService.findContactRecordPage(contactRecordQuery);

		List<SearchProject> SearchProjectlist = (List) page.getResult();
		ListRange<SearchProject> resultList = new ListRange<SearchProject>();
		resultList.setRows(SearchProjectlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	public void listproject(HttpServletRequest request, HttpServletResponse response, SearchProjectQuery searchProjectQuery) throws IOException {
		OnlineManager onlineManager = super.getOnlineManager(request, response);
		if (onlineManager != null) {
			OnlineUser onlineUser = onlineManager.getOnlineUser();
			searchProjectQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectService.findProjectPage(searchProjectQuery);

		List<SearchProject> SearchProjectlist = (List) page.getResult();
		ListRange<SearchProject> resultList = new ListRange<SearchProject>();
		resultList.setRows(SearchProjectlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	public void sllist(HttpServletRequest request, HttpServletResponse response, SearchProjectQuery searchProjectQuery) throws IOException {
		OnlineManager onlineManager = super.getOnlineManager(request, response);
		if (onlineManager != null) {
			OnlineUser onlineUser = onlineManager.getOnlineUser();
			searchProjectQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectService.findProjectPage(searchProjectQuery);

		List<SearchProject> SearchProjectlist = (List) page.getResult();
		ListRange<SearchProject> resultList = new ListRange<SearchProject>();
		resultList.setRows(SearchProjectlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	public void get(HttpServletRequest request, HttpServletResponse response, SearchProject searchProject) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SearchProject oldSearchProject = (SearchProject) searchProjectService.getById(searchProject.getId());

			if (oldSearchProject != null)
				result.put("data", oldSearchProject);
			result.put("success", true);
			result.put("msg", "成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void save(HttpServletRequest request, HttpServletResponse response, SearchProject searchProject) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(SearchProject.class, "creator")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(searchProject, "creator", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProject.class, "createDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(searchProject, "createDt", new Date());
				} catch (Exception ex) {
				}
			}

			searchProjectService.save(searchProject);
			result.put("success", true);
			result.put("msg", "添加成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response, SearchProject searchProject) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SearchProject oldsearchProject = (SearchProject) searchProjectService.getById(searchProject.getId());

			bind(request, oldsearchProject);
			// 设置修改用户和修改时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(SearchProject.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(oldsearchProject, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProject.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(oldsearchProject, "modifiedDt", new Date());
				} catch (Exception ex) {
				}
			}

			searchProjectService.update(oldsearchProject);
			result.put("success", true);
			result.put("msg", "修改成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void modifyowner(HttpServletRequest request, HttpServletResponse response, SearchProject searchProject) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SearchProject oldsearchProject = (SearchProject) searchProjectService.getById(searchProject.getId());

			bind(request, oldsearchProject);
			// 设置修改用户和修改时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(SearchProject.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(oldsearchProject, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProject.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(oldsearchProject, "modifiedDt", new Date());
				} catch (Exception ex) {
				}
			}

			searchProjectService.update(oldsearchProject);
			result.put("success", true);
			result.put("msg", "修改成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("failure", true);
			result.put("msg", e.getMessage());
		}
		outJson(response, result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser = null;
			if (onlineManager != null) {
				onlineUser = onlineManager.getOnlineUser();

			}
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer((String) idarray[i]);
				// 判断是否属于同一个人

				SearchProject searchProject = (SearchProject) searchProjectService.getById(id);
				if (onlineUser != null && searchProject != null && !onlineUser.getUsername().equals(searchProject.getAccount())) {
					result.put("failure", true);
					result.put("msg", "不是创建人，不能删除！");
					outJson(response, result);
					return;
				}
				// if (SearchProject)
				searchProjectService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void cvjoin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cvids = request.getParameter("cvids");//
		String projectids = request.getParameter("projectids"); //
		String[] cvidsarray = cvids.split(",");
		String[] projectidsidarray = projectids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		System.out.println("length:" + cvids + "--" + projectids + "==" + cvidsarray.length + "==" + projectidsidarray.length);
		try {
			String username = null;
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null)
					username = onlineUser.getUsername();
			}

			SearchProjectCv searchProjectCv = null;
			boolean hasSuccess = false;
			for (int i = 0; i < cvidsarray.length; i++) {
				for (int j = 0; j < projectidsidarray.length; j++) {
					java.lang.Integer cvid = new java.lang.Integer((String) cvidsarray[i]);
					java.lang.Integer projectid = new java.lang.Integer((String) projectidsidarray[j]);
					searchProjectCv = new SearchProjectCv();

					searchProjectCv.setProjectId(projectid);
					searchProjectCv.setCvId(cvid);
					searchProjectCv.setCreateDt(new Date());
					searchProjectCv.setCreator(username);
					System.out.println(cvid + "--" + projectid);
					boolean hasJoin = searchProjectService.cvJoinProject(searchProjectCv);
					if (hasJoin)
						hasSuccess = true;

				}
			}
			if (hasSuccess) {
				result.put("success", true);
				result.put("msg", "加入成功");
			} else {
				result.put("failure", true);
				result.put("msg", "加入失败");
			}
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void companyjoin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String companyids = request.getParameter("companyids");//
		String projectids = request.getParameter("projectids"); //
		String[] companyidsarray = companyids.split(",");
		String[] projectidsidarray = projectids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String username = null;
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null)
					username = onlineUser.getUsername();
			}

			SearchProjectCompany searchProjectCompany = null;
			boolean hasSuccess = false;
			for (int i = 0; i < companyidsarray.length; i++) {
				for (int j = 0; j < projectidsidarray.length; j++) {
					java.lang.Integer companyid = new java.lang.Integer((String) companyidsarray[i]);
					java.lang.Integer projectid = new java.lang.Integer((String) projectidsidarray[j]);
					if (searchProjectService.checkCompanyIntegrity(companyid)) {
						searchProjectCompany = new SearchProjectCompany();
						searchProjectCompany.setProjectId(projectid);
						// if (searchProjectCompany.getCompanySimple()!=null)
						// searchProjectCompany.getCompanySimple().setId(companyid);
						searchProjectCompany.setCompanyId(companyid);
						searchProjectCompany.setCreateDt(new Date());
						searchProjectCompany.setCreator(username);
						hasSuccess = searchProjectService.companyJoinProject(searchProjectCompany);
					} else {
						result.put("failure", true);
						result.put("msg", "添加失败! 公司:" + companyid + " ,简历完整度 为  '申请删除'   状态 不能添加到Search List !");
						outJson(response, result);
						return;
					}

				}
			}
			if (hasSuccess) {
				result.put("success", true);
				result.put("msg", "加入成功");
			} else {
				result.put("failure", true);
				result.put("msg", "加入失败");
			}
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void jobjoin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String jobids = request.getParameter("jobids");//
		String projectids = request.getParameter("projectids"); //
		String[] jobidsarray = jobids.split(",");
		String[] projectidsidarray = projectids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String username = null;
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null)
					username = onlineUser.getUsername();
			}

			ContractJobSearchProject contractJobSearchProject = null;
			boolean hasSuccess = false;
			for (int i = 0; i < jobidsarray.length; i++) {
				for (int j = 0; j < projectidsidarray.length; j++) {
					java.lang.Integer jobid = new java.lang.Integer((String) jobidsarray[i]);
					java.lang.Integer projectid = new java.lang.Integer((String) projectidsidarray[j]);
					contractJobSearchProject = new ContractJobSearchProject();
					contractJobSearchProject.setProjectId(projectid);
					contractJobSearchProject.setJobId(jobid);
					contractJobSearchProject.setCreateDt(new Date());
					contractJobSearchProject.setCreator(username);
					boolean hasJoin = searchProjectService.jobJoinProject(contractJobSearchProject);
					if (hasJoin)
						hasSuccess = true;

				}
			}
			if (hasSuccess) {
				result.put("success", true);
				result.put("msg", "加入成功");
			} else {
				result.put("failure", true);
				result.put("msg", "加入失败");
			}
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);

	}

	/**
	 * 检查CC项目行业 职能 是否存在相同
	 * 
	 */
	public void CheckSearchList(HttpServletRequest request, HttpServletResponse response) {

		Map<String, Object> result = new HashMap<String, Object>();

		String industrys = request.getParameter("industrys"); // 行业

		String functions = request.getParameter("functions"); // 职能

		if (StringUtils.isNotBlank(industrys) && StringUtils.isNotBlank(industrys)) {
			int count = searchProjectService.checkSearchList(industrys, functions);
			if (count == 0) {
				result.put("success", true);
				result.put("msg", "恭喜 未存在相同 行业 相同职能的项目 ");
			} else if (count == 1) {
				result.put("success", false);
				result.put("msg", "已经存在相同行业 相同职能 的项目.");
			} else if (count == 2) {
				result.put("success", false);
				result.put("msg", "已经存 2 在相同行业 相同职能 的项目.");
			} else if (count > 2) {
				result.put("success", false);
				result.put("msg", "已经存在 2  以上 相同行业 相同职能 的项目.");
			}
		}
		// searchProjectService.checkSearchList(industrys, functions);
		outJson(response, result);

	}

	/**
	 * 导出SL 报告
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void exportsearchrpeport(HttpServletRequest request, HttpServletResponse response, SearchRpeportQuery query) throws IOException {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			searchProjectService.exportSearchRpeport(query, response);
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		// outJson(response, result);
	}

}
